package com.luanye.enjoy.handler;

import com.alibaba.fastjson.JSON;
import com.luanye.enjoy.utils.WebUtils;
import com.luanye.enjoy.vo.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        //为了方便调试
        //e.printStackTrace();
        log.debug("请求权限问题，路径为： {}", httpServletRequest.getRequestURL());
//         InsufficientAuthenticationException
        //BadCredentialsException
        RespBean result = RespBean.error(99999,e.getMessage());

        //响应给前端
        WebUtils.renderString(httpServletResponse, JSON.toJSONString(result));
    }
}
